package com.wx.springboot.step9back.controller;

import com.wx.springboot.step9back.common.api.CommonResult;
import com.wx.springboot.step9back.common.util.MyDate;
import com.wx.springboot.step9back.model.Page;
import com.wx.springboot.step9back.model.Permission;
import com.wx.springboot.step9back.model.RolePermission;
import com.wx.springboot.step9back.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @PreAuthorize("hasAuthority('wx:permission:read')")
    @GetMapping("/getSelectList")
    public CommonResult getSelectList(@RequestParam Map<String, Object> data){
        HashMap<String, Object> map = new HashMap<>();
        List<Permission> permissions = permissionService.getSelectList(data);
        int count = permissionService.getSelectCount(data);
        Page page = new Page();
        page.setList(permissions);
        page.setTotal(count);
        map.put("list",permissions);
        map.put("count",count);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:permission:read')")
    @GetMapping("/getPermissionById")
    public CommonResult getPermissionById(@RequestParam Integer id){
        HashMap<String, Object> map = new HashMap<>();
        Permission permission = permissionService.getPermissionById(id);
        map.put("data",permission);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:permission:create')")
    @PostMapping("/addPermission")
    public CommonResult addPermission(@RequestBody Permission permission){
        HashMap<String, Object> map = new HashMap<>();
        String result = permissionService.addPermission(permission);
        map.put("result",result);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:permission:update')")
    @PostMapping("/updatePermission")
    public CommonResult updatePermission(@RequestBody Permission permission){
        HashMap<String, Object> map = new HashMap<>();
        int result = permissionService.updatePermission(permission);
        map.put("result",result);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:permission:delete')")
    @PostMapping("/delPermission")
    public CommonResult delPermission(@RequestBody Permission permission){
        HashMap<String, Object> map = new HashMap<>();
        int result = permissionService.delPermission(permission);
        map.put("result",result);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:permission:update')")
    @PostMapping("/updateState")
    public CommonResult updateState(@RequestBody Permission permission){
        HashMap<String, Object> map = new HashMap<>();
        int result = permissionService.updateState(permission);
        map.put("result",result);
        return CommonResult.success(map);
    }

    @PreAuthorize("hasAuthority('wx:permission:read')")
    @GetMapping("/getPermissionByRoleId")
    public CommonResult getPermissionByRoleId(@RequestParam Integer roleId){
        HashMap<String, Object> data = new HashMap<>();
        List<RolePermission> rolePermissions = permissionService.getPermissionByRoleId(roleId);
        data.put("data",rolePermissions);
        return CommonResult.success(data);
    }

    @PreAuthorize("hasAuthority('wx:permission:read')")
    @GetMapping("/getPermissionItemByNoRoleId")
    public CommonResult getPermissionItemByNoRoleId(@RequestParam Integer roleId){
        HashMap<String, Object> data = new HashMap<>();
        List<RolePermission> rolePermissions = permissionService.getPermissionItemByNoRoleId(roleId);
        data.put("data",rolePermissions);
        return CommonResult.success(data);
    }

    @PreAuthorize("hasAuthority('wx:permission:create')")
    @PostMapping("/addRolePermission")
    public CommonResult addRolePermission(@RequestBody List<RolePermission> rolePermissions){
        HashMap<String, Object> data = new HashMap<>();
        String result = permissionService.addRolePermission(rolePermissions);
        data.put("data",result);
        return CommonResult.success(data);
    }

    @PreAuthorize("hasAuthority('wx:permission:delete')")
    @PostMapping("/removeRolePermission")
    public CommonResult removeRolePermission(@RequestParam Integer roleId,@RequestParam List<Integer> ids){
        HashMap<String, Object> data = new HashMap<>();
        String result = permissionService.removeRolePermission(roleId,ids);
        data.put("data",result);
        return CommonResult.success(data);
    }

}
